'use strict';

angular.module('app')
    .controller('overMediaUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
        function ($scope, $rootScope, $state, $timeout, toaster) {
            var title = "";
            if ($state.includes('**.overMedia.update')) {
                title = "编辑媒体";
                var id = $state.params.id;
                activate(id);
            } else if ($state.includes('**.overMedia.create')) {
                title = "添加媒体";
                $scope.record = {};
                $scope.record.isEnable='Y';
                setTimeout(function () {
                    !$rootScope.$$phase && $scope.$apply();
                }, 300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            //初始化验证
            $scope.submit = function () {
                var picUrl = $scope.imgData[0];
                if (!picUrl) {
                    $scope.alertModal({'content': '请上传公司logo图'});
                    return;
                }
                var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/
                var objExp = new RegExp(Expression);
                if (objExp.test($scope.record.companyUrl) == false) {
                    $scope.alertModal({'content': '请输入正确网址信息'});
                    return true;
                }
                var m = $scope.record;
                if (m) {
                    var picUrl = $scope.imgData[0].src;
                    $scope.record.logo = picUrl[0];
                    $scope.record.type = 2; //覆盖媒体
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url: $scope.record.id ? $scope.app.project_name + '/overMedia/update' : $scope.app.project_name + '/overMedia/add',
                        data: $scope.record
                    }).then(callback);
                }
                function callback(result) {
                    if (result.httpCode == 200) {//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function () {
                            $state.go('main.web.overMedia.list');
                        }, 2000);
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                }
            }

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/overMedia/detail',
                    data: {'id': id}
                }).then(function (result) {
                    $scope.loading = false;
                    $scope.record = result.data;
                    if (result.httpCode == 200) {
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.logo);
                        $scope.imgData.push({
                            src: srcArr
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);